package com.eascs.admin.o2o.internal.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.admin.common.service.BaseSevice;
import com.eascs.admin.o2o.internal.dao.WfTypeStatusDao;
import com.eascs.admin.o2o.internal.entity.WfTypeStatus;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.app.dao.PageResult;
/**
 * 主流程---状态表
 * @author ycz
 *
 */
@Service
public class WfTypeStatusService  extends BaseSevice{
	@Autowired
	private WfTypeStatusDao wfTypeStatusDao;
	/**
	 * WfTypeStatus 列表
	 * @param wts
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	public PageResult<WfTypeStatus> queryListPage(WfTypeStatus wts, int pageSize, int pageIndex){
		return wfTypeStatusDao.queryListPage(wts,pageSize,pageIndex);
	}
	/**
	 * 添加WfTypeStatus实体
	 * @param wts
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	public void add(WfTypeStatus wts){
		wfTypeStatusDao.insert(wts);
	}
	/**
	 * 修改
	 * @param wts
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	public void update(WfTypeStatus wts){
		wfTypeStatusDao.updateById(wts);
	}
	public WfTypeStatus queryById(String id) {
		return wfTypeStatusDao.getById(WfTypeStatus.class, id);
	}
	/**
	 * 根据主表ID和WfType  查询WfTypeStatus实体
	 * @param loanapplyNo
	 * @param wfType
	 * @return
	 */
	public WfTypeStatus queryLoanapplyNo(String loanapplyNo,String wfType){
		return wfTypeStatusDao.getByLoanapplyNoAndWfType(loanapplyNo, wfType);
	}
	/**
	 * 根据主表ID查询 List<WfTypeStatus>
	 * @param loanapplyNo
	 * @return
	 */
	public List<WfTypeStatus> queryWfTypeStatus(String loanapplyNo){
		return wfTypeStatusDao.queryWfTypeStatus(loanapplyNo);
	}
	/**
	 * 驳回启动时查找出数椐并更新为RUN
	 */
	public int updatefindRUN(String loanapplyNo, String wfType){
		WfTypeStatus wfTypeStatus = wfTypeStatusDao.getByLoanapplyNoAndWfType(loanapplyNo, wfType);
		if (wfTypeStatus!=null) {
			wfTypeStatus.setWfStatus(StatusContexts.WFTYPE_STATUS_RUN);
			wfTypeStatus.setStartDate(new Date());
			return wfTypeStatusDao.updateById(wfTypeStatus);
		}
		return 0;
	}
}
